package com.ruoyi.farm.biz.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.farm.api.domain.Varieties;
import com.ruoyi.farm.biz.service.IVarietiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 品种Controller
 * 
 * @author Andy
 * @date 2022-07-19
 */
@RestController
@RequestMapping("/varieties")
public class VarietiesController extends BaseController
{
    @Autowired
    private IVarietiesService varietiesService;

    /**
     * 查询品种列表
     */
    @RequiresPermissions("farm:varieties:list")
    @GetMapping("/list")
    public TableDataInfo list(Varieties varieties)
    {
        startPage();
        List<Varieties> list = varietiesService.selectVarietiesList(varieties);
        return getDataTable(list);
    }

    @GetMapping("/all")
    public AjaxResult all(Varieties varieties){
        return AjaxResult.success(varietiesService.selectVarietiesList(varieties));
    }

    /**
     * 导出品种列表
     */
    @RequiresPermissions("farm:varieties:export")
    @Log(title = "品种", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Varieties varieties)
    {
        List<Varieties> list = varietiesService.selectVarietiesList(varieties);
        ExcelUtil<Varieties> util = new ExcelUtil<Varieties>(Varieties.class);
        util.exportExcel(response, list, "品种数据");
    }

    /**
     * 获取品种详细信息
     */
    @RequiresPermissions("farm:varieties:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(varietiesService.selectVarietiesById(id));
    }

    /**
     * 新增品种
     */
    @RequiresPermissions("farm:varieties:add")
    @Log(title = "品种", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Varieties varieties)
    {
        return toAjax(varietiesService.insertVarieties(varieties));
    }

    /**
     * 修改品种
     */
    @RequiresPermissions("farm:varieties:edit")
    @Log(title = "品种", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Varieties varieties)
    {
        return toAjax(varietiesService.updateVarieties(varieties));
    }

    /**
     * 删除品种
     */
    @RequiresPermissions("farm:varieties:remove")
    @Log(title = "品种", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(varietiesService.deleteVarietiesByIds(ids));
    }
}
